//
//  SVRootScrollView.m
//  SlideView
//
//  Created by Chen Yaoqiang on 13-12-27.
//  Copyright (c) 2013年 Chen Yaoqiang. All rights reserved.
//

#import "SVRootScrollView.h"

#import "SVGloble.h"
#import "SVTopScrollView.h"

#define POSITIONID (int)(self.contentOffset.x/[UIScreen mainScreen].bounds.size.width)

@implementation SVRootScrollView
@synthesize viewControllerArray;

+ (SVRootScrollView *)shareInstance {
    static SVRootScrollView *_instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance=[[self alloc] initWithFrame:CGRectMake(0, [[SVGloble shareInstance] globleTopHeight], [UIScreen mainScreen].bounds.size.width, [SVGloble shareInstance].globleHeight-[[SVGloble shareInstance] globleTopHeight])];
    });
    return _instance;
}

+ (void)setSelectedIndex:(NSInteger)index
{
    [SVTopScrollView setSelectedIndex:index];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate = self;
        self.backgroundColor = [UIColor clearColor];
        self.pagingEnabled = YES;
        self.userInteractionEnabled = YES;
        self.bounces = NO;
        
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        
        userContentOffsetX = 0;
    }
    return self;
}

- (void)initWithViewControllers
{
    float width = 0.0;
    userContentOffsetX = 0;
    for (UIView* view in self.subviews) {
        if (view.tag > 0) {
            [view removeFromSuperview];
        }
    }
    self.contentSize = self.bounds.size;
    self.contentOffset = CGPointZero;
    for (int i = 0; i < [viewControllerArray count]; i++)
    {
        id controller = [viewControllerArray objectAtIndex:i];
        if ([controller isKindOfClass:[UIViewController class]])
        {
            UIViewController *con = (UIViewController *)controller;
            con.view.frame = CGRectMake(width, 0, con.view.bounds.size.width, con.view.bounds.size.height);
            width += con.view.bounds.size.width;
            [self addSubview:con.view];
            con.view.tag = i+1;
        }
    }
    self.contentSize = CGSizeMake(width, /*[SVGloble shareInstance].globleHeight-[[SVGloble shareInstance] globleTopHeight]*/0);
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    userContentOffsetX = scrollView.contentOffset.x;
    self.ifDragging = YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (userContentOffsetX < scrollView.contentOffset.x) {
        isLeftScroll = YES;
    }
    else {
        isLeftScroll = NO;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //调整顶部滑条按钮状态
    [self adjustTopScrollViewButton];
    [self refleshPageTitle];
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    NSLog(@"scrollViewWillEndDragging");
    self.ifDragging = NO;
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self refleshPageTitle];
}

-(void)refleshPageTitle
{
    CGFloat pagewidth = self.frame.size.width;
    int page = floor((self.contentOffset.x - pagewidth/_pageTitleArray.count)/pagewidth)+1;
    NSString *pageTitle = [NSString stringWithFormat:@"%@",[_pageTitleArray objectAtIndex:page]];
    if (_svrDelegate != nil && [_svrDelegate respondsToSelector:@selector(svRootScrollViewDidSelectPageWithTitle:)])
    {
        [_svrDelegate svRootScrollViewDidSelectPageWithTitle:pageTitle];
    }
}

//滚动后修改顶部滚动条
- (void)adjustTopScrollViewButton
{
    [[SVTopScrollView shareInstance] setButtonUnSelect];
    [SVTopScrollView shareInstance].scrollViewSelectedChannelID = POSITIONID+100;
    [[SVTopScrollView shareInstance] setButtonSelect];
    [[SVTopScrollView shareInstance] setScrollViewContentOffset];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
